package cc.rengu.igas.channel.cups.core.service.trans;


import cc.rengu.igas.channel.cups.common.constant.CupsParamConstant;
import cc.rengu.igas.channel.cups.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.cups.core.model.CupsFieldRspBean;
import cc.rengu.igas.channel.cups.core.service.base.BasePayService;
import cc.rengu.igas.channel.cups.facade.request.TransferTrapRequest;
import cc.rengu.igas.channel.cups.facade.response.TransferTrapResponse;
import cc.rengu.igas.share.facade.bean.CupsField;
import cc.rengu.igas.share.facade.bean.CupsFieldBean;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.utility.util.AmountUtil;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;

/**
 * 非指定账户圈存
 *
 * @author Administrator
 */
public class CupsTransferTrapService extends BasePayService<TransferTrapRequest, TransferTrapResponse> {
    @Override
    protected ChnlBizResponse callBizService(CupsFieldBean cupsFieldBean,
                                             TransferTrapRequest transferTrapRequest, TransferTrapResponse transferTrapResponse) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();

        CupsField cupsField = cupsFieldBean.getCupsField();
        BeanUtil.beanCopy(transferTrapRequest, cupsField);
        cupsField.setMsgType("0200");
        cupsField.setProcCode("620000");
        cupsField.setConCode("91");

        String transDate = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.TXN_DATE);
        String transTime = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.TXN_TIME);
        String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);
        cupsField.setTransDateTime(transDate.substring(4, 8) + transTime);
        cupsField.setLocalDate(transDate.substring(4, 8));
        cupsField.setLocalTime(transTime);
        cupsField.setCupsTraceNo(sysSeqNum.substring(26, 32));
        cupsField.setReferenceNo(cupsField.getLocalTime() + cupsField.getCupsTraceNo());

        //  终端读取能力
        //  IC卡条件代码
        //  小额免密标志
        //  终端类型
        //  特殊计费类型
        cupsFieldBean.createF60()
                .setF60_2_2(transferTrapRequest.getTermRead())
                .setF60_2_3(transferTrapRequest.getIcConCode())
                .setF60_2_6(transferTrapRequest.getNoPinFlag())
                .setF60_2_5(transferTrapRequest.getTermType())
                .setF60_3_1(transferTrapRequest.getSpecialBillType());
        cupsFieldBean.build();

        String tmpAmt = AmountUtil.changeY2F(transferTrapRequest.getOrderAmt());
        cupsField.setOrderAmt(String.format("%012d", Integer.valueOf(tmpAmt)));
        cupsFieldBean.setCupsField(cupsField);
        ConvertUtil.convertInput(cupsFieldBean);

        //  调用 CUPS   TODO 此交易配置暂时有误 ,关联了行内交易码, 后续解耦
        CupsFieldRspBean cupsFieldRspBean = callCups(cupsFieldBean, transferTrapRequest.getHeader().getTxnNum());

        if (transferTrapResponse.getSettleDate() != null) {
            transferTrapResponse.setSettleDate(getCupsSettleDate(transferTrapResponse.getSettleDate()));
        }
        CupsField cupsFieldRsp = cupsFieldRspBean.getCupsFieldRsp();
        BeanUtil.beanCopy(cupsFieldRsp, transferTrapResponse);
        transferTrapResponse.setRespCode(cupsFieldRsp.getRespCode());

        if (CupsParamConstant.SUCCESS_CODE.equals(transferTrapResponse.getRespCode())) {
            transferTrapResponse.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
            transferTrapResponse.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
        }
        return new ChnlBizResponse(transferTrapResponse);

    }
}
